<?php
session_start();

if (!isset($_SESSION['username']) and !isset($_SESSION['password'])) {
    header("Location: index.php");
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Studiehjälpen</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/projectstyle.css">

    </head>
    <body>
        <div id= "container">
            <div id="header"> 
                <img border="0" src="resources/images/blue.png" id="pic1" alt="gear" width="100" height="100"></div>

            <div id= "container" name= "container" class = "container">

                <Div id= "admin" name= "admin"> 
                    <h2> Redigera dina kurser </h2>
                    <a href="courses/listcourses.php" target="content"><div class = "icgon">Lägg till lektion</div></a>
                    <a href="addz.php" target="content"><div class = "icgon">Lägg till quiz</div></a>
                    <a href="addquiz.php" target="content"><div class = "icgon">Lägg till Flashcard</div></a>
                    <a href="courses/availablecourses.php" target="content"><div class = "icgon">Redigera kurser i databasen</div></a>
                </div>

                <p id= "user" name = "user" class ="user">Inloggad som: <?php echo $_SESSION['username']; ?> <p/>
                <div id = "menu" name = "menu" class = "menu">

                    <P>Mina kurser:</P> 

                    <!--php script used to output the courses a student participate in-->
                    <?php
                    $uname = $_SESSION['username'];
                    // create a connection to mysql database
                    // $condb = mysqli_connect("localhost","root","password","PROJECT");
                    
                    $user =  $_SESSION['user'];
                    $password =  $_SESSION['password'];
                    $database = $_SESSION['database'];
                    $host= $_SESSION['host'];

                    $con = mysqli_connect($host, $user, $password, $database);

                    //verifies  that connection is succesfull if not display error message
                    if (mysqli_connect_errno()) {
                        echo "sorry! couldn't connect to database " . mysqli_connect_error();
                    }

                    //create a myslq query that selects courses associated with a specifik username
                    $result = mysqli_query($con, "select COURSE.COURSE_ID, COURSE_NAME from PERSON_TAKES_COURSE, 
                                COURSE, USER_INFO  WHERE USERNAME='$uname' AND  PERSON_TAKES_COURSE.ID = USER_INFO.ID 
                                AND PERSON_TAKES_COURSE.COURSE_ID = COURSE.COURSE_ID;");

                    while ($row = mysqli_fetch_array($result)) {
                        echo "<p>" . "<span id = \"date\">" . $row['COURSE_ID'] . "</span>" . " " . "<span id = \"sign\">" . $row['COURSE_NAME'] . "</p>";

                        echo "<form name = \"frm1\" id =\"frm1\" action=\"quizzar.php\" method=\"post\" target =\"content\">";
                        echo "<input class=\"hide\" type=\"text\" name=\"coursename\" id = \"coursename\" value=" . $row['COURSE_NAME'] . ">";
                        echo "<input class=\"hide\" type=\"text\" name=\"courseid\" id = \"courseid\" value=" . $row['COURSE_ID'] . ">";

                        echo "<input type=\"submit\" value=\"Quizzar\" class=\"butext\">";
                        echo"</form>";

                        echo "<form name = \"frm1\" id =\"frm1\" action=\"flashcards.php\" method=\"post\" target =\"content\">";
                        echo "<input class=\"hide\" type=\"text\" name=\"courseid\" id = \"courseid\" value=" . $row['COURSE_ID'] . ">";
                        echo "<input class=\"hide\" type=\"text\" name=\"coursename\" id = \"coursename\" value=" . $row['COURSE_NAME'] . ">";
                        echo "<input type=\"submit\" value=\"Flashcards\" class=\"butext\">";
                        echo"</form>";
                    }
                    mysqli_close($con); // close connection
                    ?>

                </div>
                <iframe name = "content" id = "content" class = "content" src="startframe.php" scrolling="yes">
                <p>Your browser does not support iframes.</p>
                </iframe> 
            </div>
        </div>
    </body>
</html>